using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;

namespace KWSResourceBooking.Features.Lists
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("29014408-ca26-4763-be94-4ddc1f77f555")]
    public class ListsEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            using (SPWeb web = (SPWeb)properties.Feature.Parent)
            {
                SPListTemplate templateResources = web.ListTemplates["KWS Resources"];
                Guid guidResources = web.Lists.Add("KWS Resources", "List of resources available for booking.", "KWSResources", properties.Feature.DefinitionId.ToString(), 10002, "100", SPListTemplate.QuickLaunchOptions.On);
                SPList listResources = web.Lists[guidResources];
                listResources.OnQuickLaunch = true;
                listResources.Update();
                addResource(listResources, "Calculator");
                addResource(listResources, "Pencil Sharpener");
                addResource(listResources, "Ruler");
                addResource(listResources, "Stapler");

                SPListTemplate templateBookings = web.ListTemplates["KWS Bookings"];
                Guid guidBookings = web.Lists.Add("KWS Bookings", "List of resource bookings.", "KWSBookings", properties.Feature.DefinitionId.ToString(), 10003, "100", SPListTemplate.QuickLaunchOptions.Off);
                SPList listBookings = web.Lists[guidBookings];
                listBookings.OnQuickLaunch = false;
                listBookings.Update();
            }
        }

        private void addResource(SPList list, string Title)
        {
            SPListItem itemResource = list.Items.Add();
            itemResource["Title"] = Title;
            itemResource.SystemUpdate();
        }


        // Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            using (SPWeb web = (SPWeb)properties.Feature.Parent)
            {
                SPList listResources = web.Lists.TryGetList("KWS Resources");
                if (listResources != null) listResources.Delete();

                SPList listBookings = web.Lists.TryGetList("KWS Bookings");
                if (listBookings != null) listBookings.Delete();
            }
        }


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
